<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>boardshopView.jsp</title>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<SCRIPT type="text/javascript">

<c:if test="${!empty msg }">
alert("${msg }");
</c:if>

	function successFun(result){
		var state = result.form.state;
		if(state=="success"){
			var html="";
			$.each(result.form.dataList, function(i, value){
				html += "<tr>\n";
				var rsh_no = value.rsh_no;
				var rsh_writer = value.rsh_writer;
				var rsh_content = value.rsh_content;
				var rsh_pass = value.rsh_pass;
				var rsh_date = value.rsh_date;
				var delBtn = "<img src='${cPath }/css/del.png'"
							 + " onclick='deleteFun(" + rsh_no + ")'  />";
				html += "<td width='8%'>" + rsh_writer +"<span style='color: red;'></span></td><td width='60%' id='left'>" 
						+ rsh_content + "</td><td width='16%'>" + rsh_date + "</td><td width='5%'>" + delBtn + "</td>";
				html += "</tr>\n";
			});
			
			$("#rshList_body").html(html);
			var paging = result.form.pagingHTML;
			$("#paging").html(paging);
		}else {
			alert(result.form.message);
		}
		$("#rsh_writer").val("");
		$("#rsh_pass").val("");
		$("#rsh_content").val("");
	}

	function deleteFun(rsh_no){ // 댓글 삭제를 위한 서비스 호출
		var pass = prompt("암호를 달라옹~");
		//$("#del_re_no").val(re_no);
		//$("#del_re_pass").val(pass);
		var rsh_ref_key = $("#rsh_ref_key").val();
		if(pass==null || pass==""){
			return false;
		}
		var params = "rsh_no=" + rsh_no + "&rsh_pass=" + pass + "&rsh_ref_key=" + rsh_ref_key;
		$.ajax({
			url : "${cPath}/replyshopDelete.do",
			data : params,
			dataType : "json",
			//success : successDel,
			success : successFun,
			error : function(xhr){
				 alert("상태코드 : " + xhr.status +" 에러발생 ~ 잠시뒤 다시 시도!!");
			}
			, 
			resetForm : true
		});			
		//$("#deleteForm").trigger("submit");
	}
	
	$(document).ready(function(){
		goPage(1);
		/*
		$("#replyForm").ajaxForm({ // 댓글 저장을 위한 서비스 호출
				dataType : "json", 
				success : successFun,
				error : function(xhr){
					 alert("상태코드 : " + xhr.status +" 에러발생 ~ 잠시뒤 다시 시도!!");
				}, 
				resetForm : true
		});
		*/ 
	});
	
	function insertReplyShop(){
		
		var rsh_ref_key = $("#rsh_ref_key").val();
		var rsh_writer = $("#rsh_writer").val(); 
		var rsh_pass = $("#rsh_pass").val(); 
		var rsh_content = $("#rsh_content").val();
		
		var params =  "rsh_ref_key=" + rsh_ref_key + "&rsh_writer=" + rsh_writer + "&rsh_pass=" + rsh_pass + "&rsh_content=" + rsh_content;
		$.ajax({ // 댓글 저장을 위한 서비스 호출
			url : "${cPath}/replyshopInsert.do",
			data : params,
			dataType : "json", 
			success : successFun,
			error : function(xhr){
				 alert("상태코드 : " + xhr.status +" 에러발생 ~ 잠시뒤 다시 시도!!");
			}
		});	
		
	}
	
	function goPage(currPage){
		$.ajax({
			url : "${cPath}/replyshopList.do",
			data : "currentPage="+currPage+"&bsh_no=${boardshop.bsh_no}",
			dataType : "json",
			success : successFun,
			error : function(xhr){
				 alert("상태코드 : " + xhr.status +" 에러발생 ~ 잠시뒤 다시 시도!!");
			}
		});
	}
	
	function deleteBoardshop() {
		var bsh_pass = prompt("암호 입력");
		var regexp = /^\s*$/;
		if(regexp.test(bsh_pass)) {
			alert("암호를 다시 입력하세요");
			return false;
		} else {
			window.location.href="${cPath}/boardshopDelete.do?bsh_no=${boardshop.bsh_no}&bsh_pass="+bsh_pass;
		}
	}
	

</script>
</head>
<body>
<div id="imgt3">
	<img src="${cPath }/css/b2.png" width="35%" align="left"><br>
	<br><br><br><br>
	</div>
<c:url value="/boardshopEdit.do" var="boardshopEdit">
	<c:param name="bsh_no" value="${boardshop.bsh_no }" />
</c:url>
<c:url value="/boardshopList.do" var="boardshopList" />
<c:url value="/boardshopDelete.do" var="boardshopDelete">
	<c:param name="bsh_no" value="${boardshop.bsh_no }" />
</c:url>

<div align="center" id="boardForm2">
<table >
	<tr>
		<th width="30%">글번호</th>
		<td id="left">${boardshop.bsh_no }</td>
	</tr>
	<tr>
		<th width="30%">제목</th>
		<td id="left">${boardshop.bsh_title }</td>
	</tr>
	<tr>
		<th width="30%">작성자</th>
		<td id="left">${boardshop.bsh_writer }</td>
	</tr>
	<tr>
		<th width="30%">내용</th>
		<td id="left">${boardshop.bsh_content }</td>
	</tr>
	
	
</table><br>
			<input type="button" value="수정하기" onclick="window.location.href='${boardshopEdit }'"/>
			<input type="button" value="목록가기" onclick="window.location.href='${boardshopList }'"/>
</div>
<br><br>

<div align="center" id="boardForm2">
		<!--댓글 리스트-->
		<table>
			<tbody id="rshList_body">
				<c:forEach var="replyshop" items="${boardshop.replyshopList }">
					<tr>
						<td>${replyshop.rsh_writer }<span style='color: red;'>${replyshop.rsh_pass }</span></td>
						<td>${replyshop.rsh_content }</td>
						<td>${replyshop.rsh_date }</td>
						<td>
							<img src='${cPath }/css/del.png' 
									onclick='deleteFun(${replyshop.rsh_no})' />
						</td>					
					</tr>
				</c:forEach>
			
			</tbody>
		</table>
		<div id="paging" ></div>
	</div>
	<br>
	<div align="center" id="boardForm2">
		<!-- 댓글 입력 폼 -->
		<form action="${cPath}/replyshopInsert.do" 
				method="post" id="replyshopForm">
			<input type="hidden" name="rsh_ref_key" id="rsh_ref_key" value="${boardshop.bsh_no }" />
			작성자 : <input type="text" name="rsh_writer" id="rsh_writer" /> 
			비밀번호 : <input type="text" name="rsh_pass" id="rsh_pass" /><br />
			<textarea rows="3" cols="75" name="rsh_content" id="rsh_content"></textarea>
			<br>
			<input type="button" value="댓글남기기"  onclick="insertReplyShop()" />
		</form>
	</div>
</body>
</html>